package io.zhengsh.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo {

    static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t ======= 外层");
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + "\t ======= 内层");
                } finally {
                    lock.unlock();
                }

            } finally {
                // 注意：通常情况下加几次锁就要解几次，不然会导致其他线程一直等待无法获取锁
                lock.unlock();
            }
        }, "t1").start();

        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t ======= 调用成功");
            } finally {
                lock.unlock();
            }
        }, "t2").start();
    }
}
